package com.fzh.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fzh.mapper.ImagesMapper;
import com.fzh.mapper.ProductMapper;
import com.fzh.pojo.dto.ProductLinkImageDTO;
import com.fzh.pojo.entity.Images;
import com.fzh.pojo.entity.Product;
import com.fzh.pojo.entity.ProductType;
import com.fzh.pojo.utils.ResponeMessageUtil;
import com.fzh.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private ProductMapper productMapper;
	
	@Autowired
	private ImagesMapper imagesMapper;

	@Override
	public List<Product> queryAllProduct(Integer setValue,Integer limit,String name,String f_ptid,String isSoldOut,String isRecommendMain) {
		return productMapper.selectAllProduct(setValue,limit,name,f_ptid,isSoldOut,isRecommendMain);
	}

	@Override
	public int queryProductCounts(String name,String f_ptid,String isSoldOut,String isRecommendMain) {
		return productMapper.selectProductCounts(name,f_ptid,isSoldOut,isRecommendMain);
	}

	@Override
	public Boolean addProduct(Product product) {
		Integer resMsg = productMapper.insertProduct(product);
		Images images = new Images();
		images.setImg(product.getImgMain());
		images.setF_pid(product.getId());
		images.setIsMain(1);
		imagesMapper.insertImages(images);
		return resMsg > 0 ? true:false;
	}

	@Override
	public Boolean editProductById(Product product) {
		Images images = new Images();
		images.setImg(product.getImgMain());
		images.setF_pid(product.getId());
		images.setIsMain(1);
		Integer resMsg = productMapper.updateProductById(product);
		imagesMapper.updateImagesByF_pid(images);
		return resMsg > 0 ? true:false;
	}

	@Override
	public Boolean delAllProductByIds(String id) {
		String[] split = id.split(",");
		List<String> ids = new ArrayList<>();
		for(int i = 0; i < split.length; i ++) {
			ids.add(split[i]);
		}
		return ResponeMessageUtil.getResMsg(productMapper.deleteAllProductByIds(ids));
	}

	@Override
	public Boolean delProductById(Integer id) {
		return ResponeMessageUtil.getResMsg(productMapper.deleteProductById(id));
	}

	@Override
	public Boolean editProductIsSoldOutSwitchById(Integer id, Integer number) {
		return ResponeMessageUtil.getResMsg(productMapper.updateProductIsSoldOutSwitchById(id,number));
	}

	@Override
	public Boolean editProductIsDelSwitchById(Integer id, Integer number) {
		return ResponeMessageUtil.getResMsg(productMapper.updateProductIsDelSwitchById(id,number));
	}

	@Override
	public Boolean editProductIsRecommendMainSwitchById(Integer id, Integer number) {
		return ResponeMessageUtil.getResMsg(productMapper.updateProductIsRecommendMainSwitchById(id,number));
	}

	@Override
	public Boolean editProductIsRollImgSwitchById(Integer id, Integer number) {
		return ResponeMessageUtil.getResMsg(productMapper.updateProductIsRollImgSwitchById(id,number));
	}

	@Override
	public List<Product> queryAllProductWitchImagesSelect() {
		return productMapper.selectAllProductWitchImagesSelect();
	}

	@Override
	public Product queryProductByIdWitchCartInput(Integer id) {
		return productMapper.selectProductByIdWitchCartInput(id);
	}

	@Override
	public void editProductImgMainById(Integer f_pid, String img) {
		productMapper.updateProductImgMainById(f_pid,img);
	}

	@Override
	public List<Product> queryAllProduct() {
		return productMapper.selectAllProductF();
	}

	@Override
	public Boolean queryProductTypeBelowIsProductById(Integer id) {
		//查询出是否有商品，如有就返回true
		List<Product> products = productMapper.selectProductTypeBelowIsProductById(id);
		if(products.size() > 0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<Product> queryAllNewProsuctByIdDescF(int i, int j) {
		return productMapper.selectAllNewProsuctByIdDescF(i,j);
	}

	@Override
	public List<Product> selectBannerPro() {
		return productMapper.selectBannerPro();
	}

	@Override
	public List<Product> selectProductTJ() {
		return productMapper.selectProductTJ();
	}

	@Override
	public List<ProductLinkImageDTO> selectProductDetail(int id) {
		return productMapper.selectProductDetail(id);
	}

	@Override
	public Product selectProductDetailXX(int id) {
		return productMapper.selectProductDetailXX(id);
	}

	@Override
	public List<Product> selectProductTypeList(Integer id) {
		return productMapper.selectProductTypeList(id);
	}

	@Override
	public List<Product> selectProductTypeListMh(Product product) {
		return productMapper.selectProductTypeListMh(product);
	}
	
	/**
	 * {前台}查询所有商品类型用于展示,根据p_id查询一级分类
	 * @return
	 */
	


}
